package com.lxt.cloud.user.controller;



import javax.annotation.Resource;

import com.lxt.cloud.permission.entity.Permission;
import com.lxt.cloud.permission.service.IPermissionService;
import com.lxt.cloud.role.entity.Role;
import com.lxt.cloud.role.service.IRoleService;
import com.lxt.common.log.LogCode;
import com.lxt.common.mvc.msg.output.ApiResult;
import com.lxt.common.util.BeanUtils;
import com.lxt.common.vo.RoleModel;
import com.lxt.common.vo.UserModel;
import jdk.nashorn.internal.objects.annotations.Property;
import org.apache.commons.compress.utils.Lists;
import org.omg.CORBA.UserException;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import com.lxt.common.mvc.BaseController;
import com.lxt.cloud.user.service.IUserService;
import com.lxt.cloud.user.entity.User;

import java.time.LocalTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * by cssca-sdt code generator 1.0
 * @author 
 * @email 
 * @date 2021-08-05 13:15:15
 */
@Slf4j
@RestController
@RequestMapping("user")
public class UserController extends BaseController<User,String> {
	@Resource
	IUserService userService;

	@Resource
	IRoleService roleService;

	@Resource
	IPermissionService permissionService;

	@GetMapping("/check_login")
	@LogCode("USER_LOGIN")
	public ApiResult<UserModel> checkLogin(@RequestParam("userName") String userName) throws UserException {
		User user=userService.findByName(userName);
		List<Role> roleList=roleService.findRoleByUserId(user.getId());
		List<RoleModel> roleModelList= Lists.newArrayList();
		List<Long> roleIdList= Lists.newArrayList();
		List<Permission> permissionList= Lists.newArrayList();
		for(Role role:roleList){
			RoleModel roleModel=new RoleModel();
			BeanUtils.copyProperties(role,roleModel);
			roleModelList.add(roleModel);
			roleIdList.add(role.getId());
		}
		UserModel userModel=new UserModel();
		BeanUtils.copyProperties(user,userModel);
		userModel.setRoleModelList(roleModelList);
//		permissionList=permissionService.findPermissionByRoleIds(roleIdList);
//		List<String> permissionCodes = permissionList.stream().map(e -> e.getCode()).collect(Collectors.toList());
//		userModel.setResourceCode(permissionCodes);
		return ApiResult.ok(userModel);
	}

	@GetMapping("/getUser")
	public ApiResult<UserModel> getUser(@RequestParam String userName) {
		User user = userService.findByName(userName);
		UserModel userModel=new UserModel();
		BeanUtils.copyProperties(user,userModel);
		return ApiResult.ok();
	}
	@GetMapping("/testsleuth")
	public String testsleuth(){
		LocalTime t= LocalTime.now();
		System.out.println("调用时间："+ t);
		return "调用时间："+ t;
	}
}